package cn.edu.ujs.coor.controller;

import cn.edu.ujs.coor.common.annotation.DataLevel;
import cn.edu.ujs.coor.common.exception.CoorException;
import cn.edu.ujs.coor.model.Project;
import cn.edu.ujs.coor.model.User;
import cn.edu.ujs.coor.request.IdRequest;
import cn.edu.ujs.coor.request.project.ProjectAddRequest;
import cn.edu.ujs.coor.request.project.ProjectPageRequest;
import cn.edu.ujs.coor.request.project.ProjectSubmitRequest;
import cn.edu.ujs.coor.request.project.ProjectUpdateRequest;
import cn.edu.ujs.coor.response.JsonResponse;
import cn.edu.ujs.coor.response.PageResponse;
import cn.edu.ujs.coor.response.ProjectResponse;
import cn.edu.ujs.coor.service.ProjectService;
import cn.edu.ujs.coor.util.JwtUnit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping(value = "/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @RequestMapping(value = "/info")
    public JsonResponse info (@RequestBody @Valid IdRequest request, User user) {
        ProjectResponse response = projectService.info(request, user);
        return JsonResponse.success(response);
    }

    @RequestMapping(value = "/create")
    public JsonResponse create (@RequestBody @Valid ProjectAddRequest request, User user) {
        request.setUserId(user.getId());
        request.setOrganizationId(user.getOrganizationId());
        projectService.create(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/update")
    public JsonResponse update (@RequestBody @Valid ProjectUpdateRequest request, User user) {
        request.setUserId(user.getId());
        projectService.update(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/delete")
    public JsonResponse delete (@RequestBody @Valid IdRequest request) {
        projectService.delete(request);
        return JsonResponse.success();
    }

    @DataLevel
    @RequestMapping(value = "/list/creator")
    public JsonResponse listByCreator (@RequestBody ProjectPageRequest request, User user) {
        PageResponse response = projectService.page(request);
        return JsonResponse.success(response);
    }

    @RequestMapping(value = "/list/actor")
    public JsonResponse listByActor (@RequestBody ProjectPageRequest request, User user) {
        request.setUserId(user.getId());
        PageResponse response = projectService.pageByActor(request);
        return JsonResponse.success(response);
    }

    @RequestMapping(value = "/submit")
    public JsonResponse submit (@RequestBody @Valid ProjectSubmitRequest request, User user) {
        request.setUserId(user.getId());
        projectService.submit(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/complete")
    public JsonResponse complete (@RequestBody @Valid IdRequest request) {
        projectService.complete(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/export/{id}")
    public void test (HttpServletResponse response, @PathVariable String id, @RequestParam String token) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        if (StringUtils.isBlank(token)) {
            throw new CoorException("未验证请求");
        }
        try {
            Map info = JwtUnit.decode(token);
            if (info.size() == 0) {
                throw new CoorException("登录信息失效");
            }
        } catch (Exception e) {
            throw new CoorException("登录信息失效");
        }
        ByteArrayOutputStream baos = projectService.export(id);
        try {
            Project project = projectService.findById(id);
            String filename = sdf.format(new Date()) + "-" + project.getName() + ("0".equals(project.getType()) ? ".xls" : ".zip");
            response.setHeader("content-disposition", "attachment;filename="
                    + URLEncoder.encode(filename, "UTF-8"));
            response.getOutputStream().write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
